package cn.iocoder.yudao.module.pay.controller.app.premium;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderRespVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitRespVO;
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import cn.iocoder.yudao.module.pay.service.premium.PayPremiumService;
import cn.iocoder.yudao.module.wine.api.dto.PremiumRecordDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.CHANNEL_NOT_FOUND;

/**
 * @author zyc
 * @createDate 2024/5/13 17:18
 */

@Tag(name = "用户 APP - 担保费模块")
@RestController
@RequestMapping("/pay/premium")
@Validated
@Slf4j
public class AppPayPremiumController {

    @Resource
    private PayPremiumService payPremiumService;

    @GetMapping("/record")
    @Operation(summary = "获得保费信息")
    @Parameter(name = "orderId", description = "编号", required = true, example = "1024")
    public CommonResult<PremiumRecordDTO> getPremiumRecord(@RequestParam("orderId") Long orderId){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return CommonResult.success(payPremiumService.getPremiumRecord(orderId));
    }

    @PostMapping("/pay-premium")
    @Operation(summary = "支付保费")
    @Parameter(name = "premiumId", description = "担保费id", required = true, example = "1024")
    public CommonResult<PayOrderSubmitRespVO> payPremium(@RequestBody PayOrderSubmitReqVO reqVO){
        return CommonResult.success(payPremiumService.payPremium(reqVO));
    }

    @PostMapping("/pay-membership-fees")
    @Operation(summary = "开通会员")
    public CommonResult<PayOrderSubmitRespVO> payMembershipFees(@RequestBody PayOrderSubmitReqVO reqVO){
        return CommonResult.success(payPremiumService.payMembershipFees(reqVO));
    }

    @PostMapping("/pay-team-fees")
    @Operation(summary = "团长缴费")
    public CommonResult<PayOrderSubmitRespVO> payTeamFees(@RequestBody PayOrderSubmitReqVO reqVO){
        return CommonResult.success(payPremiumService.payTeamFees(reqVO));
    }



    @PostMapping(value = "/order/{channelId}")
    @Operation(summary = "开通会员【支付】回调")
    @PermitAll
    @OperateLog(enable = false) // 回调地址，无需记录操作日志
    public String notifyOrder(@PathVariable("channelId") Long channelId,
                              @RequestParam(required = false) Map<String, String> params,
                              @RequestBody(required = false) String body) {
        log.info("[notifyOrder][channelId({}) 回调数据({}/{})]", channelId, params, body);
        // 1. 校验支付渠道是否存在
        PayClient payClient = payPremiumService.getPayClient(channelId);
        if (payClient == null) {
            log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
            throw exception(CHANNEL_NOT_FOUND);
        }

        // 2. 解析通知数据
        PayOrderRespDTO notify = payClient.parseOrderNotify(params, body);
        payPremiumService.notifyOrder(channelId, notify);
        return "success";
    }

    @PostMapping(value = "/pay-premium/{channelId}")
    @Operation(summary = "支付保费【支付】回调")
    @PermitAll
    @OperateLog(enable = false) // 回调地址，无需记录操作日志
    public String notifyPremium(@PathVariable("channelId") Long channelId,
                              @RequestParam(required = false) Map<String, String> params,
                              @RequestBody(required = false) String body) {
        log.info("[notifyOrder][channelId({}) 回调数据({}/{})]", channelId, params, body);
        // 1. 校验支付渠道是否存在
        PayClient payClient = payPremiumService.getPayClient(channelId);
        if (payClient == null) {
            log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
            throw exception(CHANNEL_NOT_FOUND);
        }

        // 2. 解析通知数据
        PayOrderRespDTO notify = payClient.parseOrderNotify(params, body);
        payPremiumService.notifyPremium(channelId, notify);
        return "success";
    }

    @PostMapping(value = "/guarantee/{channelId}")
    @Operation(summary = "团长缴费【支付】回调")
    @PermitAll
    @OperateLog(enable = false) // 回调地址，无需记录操作日志
    public String notifyGuarantee(@PathVariable("channelId") Long channelId,
                                @RequestParam(required = false) Map<String, String> params,
                                @RequestBody(required = false) String body) {
        log.info("[notifyOrder][channelId({}) 回调数据({}/{})]", channelId, params, body);
        // 1. 校验支付渠道是否存在
        PayClient payClient = payPremiumService.getPayClient(channelId);
        if (payClient == null) {
            log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
            throw exception(CHANNEL_NOT_FOUND);
        }

        // 2. 解析通知数据
        PayOrderRespDTO notify = payClient.parseOrderNotify(params, body);
        payPremiumService.notifyGuarantee(channelId, notify);
        return "success";
    }
}
